// pages/index/feature/feature.js
const db = wx.cloud.database();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    details: [],  
    isCollected: false,  
    envVersion:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const id = options._id; 
    const app = getApp();
    const envVersion = app.globalData.envVersion;
    this.getNewsDetail(id); 
    this.checkIfCollected(id); 
    this.setData({
      envVersion:envVersion
    })
  },

  getNewsDetail: function (id) {
    db.collection('feature').doc(id).get().then(res => {
      let details = res.data;
      
      
      const timestamp = details.updatedAt;
      const formattedTime = this.formatTime(timestamp); 
  
     
      details.formattedTime = formattedTime;
  
      // 更新页面数据
      this.setData({
        details: details
      });
    }).catch(err => {
      console.error(err);
    });
  },
  
  // 转换时间戳为可读日期格式
  formatTime: function (timestamp) {
    const date = new Date(timestamp);
    const year = date.getFullYear();
    const month = date.getMonth() + 1; 
    const day = date.getDate();
    const hours = date.getHours();
    const minutes = date.getMinutes();
    const seconds = date.getSeconds();
  
    // 返回格式化后的时间
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  },
  

  
  checkIfCollected: function (id) {
    try {
      const collectedItems = wx.getStorageSync('collectedItems') || [];
      const isCollected = collectedItems.some(item => item._id === id);
      this.setData({
        isCollected: isCollected
      });
    } catch (e) {
      console.error("检查收藏状态失败", e);
    }
  },

  
  onCollect: function () {
    const details = this.data.details;
    let isCollected = this.data.isCollected;

    if (details && Object.keys(details).length) {
      try {
        let collectedItems = wx.getStorageSync('collectedItems') || [];
        
        if (!isCollected) {
          collectedItems.push(details);
          wx.setStorageSync('collectedItems', collectedItems);
          wx.showToast({
            title: '收藏成功',
            icon: 'success'
          });
        } else {
          collectedItems = collectedItems.filter(item => item._id !== details._id);
          wx.setStorageSync('collectedItems', collectedItems);
          wx.showToast({
            title: '取消收藏',
            icon: 'success'
          });
        }

        this.setData({
          isCollected: !isCollected
        });

      } catch (e) {
        console.error("收藏操作失败", e);
        wx.showToast({
          title: '操作失败',
          icon: 'none'
        });
      }
    } else {
      wx.showToast({
        title: '数据加载中',
        icon: 'none'
      });
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */

  onShareAppMessage() {
    return {
      title: this.data.details.title || '分享标题', 
      path: '/pages/index/feature/feature?_id=' + this.data.details._id, 
      imageUrl: this.data.details.image || 'https://example.com/default-image.png', 
    };
  }
})
